/*************************************************************************
	Tag Handlers
	Call Tag functions from Scenario by Reflection
*************************************************************************/

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class UKagTagHandler {
#region Internal
	private static UKagSystem m_System = null;
	private static UKagStorages m_Storages = null;
	private static object[] tagParams = new object[1];
	
	private static Dictionary<string, MethodInfo> tagHandlers = new Dictionary<string, MethodInfo>();
	private static string[] tagNames = null;
	public static string[] GetTagNames() { return tagNames; }
	public static void InitTagHandlers()
	{
		m_System = UKagSystem.Instance;
		if (m_System != null)
			m_Storages = m_System.Storages as UKagStorages;
		UKagExtTagHandler.InitExtTagHandlers();
		
		Type type = typeof(UKagTagHandler);
		Type typeExt = typeof(UKagExtTagHandler);
		
		tagHandlers.Clear();
		ParameterInfo[] parameters;
		List<string> tagNameList = new List<string>();
		MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
		string paramName = "";
		for (int i=0; i<methods.Length; i++)
		{
			MethodInfo method = methods[i];
			parameters = method.GetParameters();
			if (parameters.Length == 1 && parameters[0].ParameterType == typeof(UKagScenarioTag))
			{
				paramName = method.Name;
				if (paramName[0] == '_')
					paramName = paramName.Substring(1);
				tagNameList.Add(paramName);
				tagHandlers.Add(paramName, method);
			}
		}
		
		methods = typeExt.GetMethods(BindingFlags.Public | BindingFlags.Static);
		for (int i=0; i<methods.Length; i++)
		{
			MethodInfo method = methods[i];
			parameters = method.GetParameters();
			if (parameters.Length == 1 && parameters[0].ParameterType == typeof(UKagScenarioTag))
			{
				paramName = method.Name;
				if (paramName[0] == '_')
					paramName = paramName.Substring(1);
				if (tagHandlers.ContainsKey(paramName))
					tagHandlers.Remove(paramName);
				else
					tagNameList.Add(paramName);
				tagHandlers.Add(paramName, method);
			}
		}
		
		tagNames = tagNameList.ToArray();
	}
	
	// for Editor Only
	public static MethodInfo GetTagHandler(string tagName)
	{
		if (!tagHandlers.ContainsKey(tagName))
			return null;
		return tagHandlers[tagName];
	}
	
	protected internal static int execTag(UKagScenarioTag tag)
	{
		tagParams[0] = tag;
		if (tagHandlers.ContainsKey(tag.name))
			return (int)tagHandlers[tag.name].Invoke(null, tagParams);
		else
			UKagSystem.LogError("Tag handler " + tag.name + " NOT found!");
		return 0;
	}
#endregion
#region Dummy
/**********************************************Dummy**************************************************/	
	[UKagTagHandlerParamAttribute("text", IsOptional=false)]
	[UKagTagHandlerParamAttribute("pagename")]
	public static int _label(UKagScenarioTag tag) { return 0; }
	
	[UKagTagHandlerParamAttribute("text", IsOptional=false)]
	public static int _comment(UKagScenarioTag tag) { return 0; }
	
	[UKagTagHandlerParamAttribute("storage")]
	[UKagTagHandlerParamAttribute("target")]
	public static int _jump(UKagScenarioTag tag) { return 0; }

	[UKagTagHandlerParamAttribute("storage")]
	[UKagTagHandlerParamAttribute("target")]
	public static int _call(UKagScenarioTag tag) { return 0; }

	[UKagTagHandlerParamAttribute("storage")]
	[UKagTagHandlerParamAttribute("target")]
	public static int _return(UKagScenarioTag tag) { return 0; }

	[UKagTagHandlerParamAttribute("exp", IsOptional=false)]
	public static int _if(UKagScenarioTag tag) { return 0; }
	
	[UKagTagHandlerParamAttribute("exp", IsOptional=false)]
	public static int _elsif(UKagScenarioTag tag) { return 0; }
	
	public static int _else(UKagScenarioTag tag) { return 0; }
	
	public static int _endif(UKagScenarioTag tag) { return 0; }
#endregion
#region System
/**********************************************System**************************************************/	
	
	[UKagTagHandlerParamAttribute("exp", IsOptional=false)]
	public static int eval(UKagScenarioTag tag)
	{
#if !UNITY_WEBPLAYER
		//string exp = tag.GetParamValue("exp");
		//if(exp != null)
		//	UKagEvaluator.Run(exp);
#endif
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("time", IsOptional=false)]
	[UKagTagHandlerParamAttribute("canskip")]
	public static int wait(UKagScenarioTag tag)
	{
		string timeStr = tag.GetParamValue("time");
		string canskip = tag.GetParamValue("canskip");
		if (canskip != null)
			m_System.conductor.SkipDisableOnce = canskip == "false";
		
		int time = 0;
		m_System.Str2Number(timeStr, ref time);
		
		return time;
	}
	
	public static int waitclick(UKagScenarioTag tag)
	{
		m_System.conductor.WaitTrigger("_click", true);
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("enabled", IsOptional=false)]
	public static int clickskip(UKagScenarioTag tag)
	{
		string enabled = tag.GetParamValue("enabled");
		m_System.conductor.CanSkip = m_System.Str2Bool(enabled);
		return 0;
	}	
	
	public static int shutdown(UKagScenarioTag tag)
	{
		m_System.conductor.Stop();
		Application.Quit();
		return 0;
	}
	
	public static int s(UKagScenarioTag tag)
	{
		m_System.conductor.Stop();
		return 0;
	}

	[UKagTagHandlerParamAttribute("exp", IsOptional=false)]
	public static int emb(UKagScenarioTag tag)
	{
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("index", IsOptional=false)]
	public static int save(UKagScenarioTag tag)
	{
#if !UKAG_READONLY
		string index = tag.GetParamValue("index");
		if (index != null && index != "")
			m_System.Save(Int32.Parse(index));
#endif
		return 0;
	}
#endregion
#region Layer
/************************************************Layer*****************************************************/
	private static UKagLayer GetLayerByTagParam(UKagScenarioTag tag)
	{
		string layer = tag.GetParamValue("layer");
		return m_System.GetLayerByName(layer);
	}

	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	[UKagTagHandlerParamAttribute("page")]
	[UKagTagHandlerParamAttribute("storage", IsOptional=false)]
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("imageleft")]
	[UKagTagHandlerParamAttribute("imagetop")]
	[UKagTagHandlerParamAttribute("imagewidth")]
	[UKagTagHandlerParamAttribute("imageheight")]
	[UKagTagHandlerParamAttribute("slice9left")]
	[UKagTagHandlerParamAttribute("slice9top")]
	[UKagTagHandlerParamAttribute("slice9right")]
	[UKagTagHandlerParamAttribute("slice9bottom")]
	[UKagTagHandlerParamAttribute("animseq")]
	[UKagTagHandlerParamAttribute("animidx")]
	[UKagTagHandlerParamAttribute("fliplr")]
	[UKagTagHandlerParamAttribute("flipud")]
	[UKagTagHandlerParamAttribute("origin")]
	[UKagTagHandlerParamAttribute("hittype")]
	[UKagTagHandlerParamAttribute("hitthreshold")]
	[UKagTagHandlerParamAttribute("index")]
	[UKagTagHandlerParamAttribute("opacity")]
	[UKagTagHandlerParamAttribute("color")]
	[UKagTagHandlerParamAttribute("materialcolor")]
	[UKagTagHandlerParamAttribute("visible")]
	public static int image(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		string storage = tag.GetParamValue("storage");
		if (storage == null || layerObj == null)
		{
			return 0;
		}
		UTVPImage img = m_Storages.GetImage(storage);
		if (img == null)
		{
			return 0;
		}
		bool isBack = tag.GetParamValue("page") == "back";
		
		if (isBack)
			layerObj.LoadBackImage(img);
		else
			layerObj.LoadImage(img);
		
		layopt(tag);
		
		UKagSystem.Log("image " + storage + " on Layer " + layerObj.name);
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	[UKagTagHandlerParamAttribute("page")]
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("imageleft")]
	[UKagTagHandlerParamAttribute("imagetop")]
	[UKagTagHandlerParamAttribute("imagewidth")]
	[UKagTagHandlerParamAttribute("imageheight")]
	[UKagTagHandlerParamAttribute("slice9left")]
	[UKagTagHandlerParamAttribute("slice9top")]
	[UKagTagHandlerParamAttribute("slice9right")]
	[UKagTagHandlerParamAttribute("slice9bottom")]
	[UKagTagHandlerParamAttribute("animseq")]
	[UKagTagHandlerParamAttribute("animidx")]
	[UKagTagHandlerParamAttribute("fliplr")]
	[UKagTagHandlerParamAttribute("flipud")]
	[UKagTagHandlerParamAttribute("origin")]
	[UKagTagHandlerParamAttribute("vorigin")]
	[UKagTagHandlerParamAttribute("hittype")]
	[UKagTagHandlerParamAttribute("hitthreshold")]
	[UKagTagHandlerParamAttribute("index")]
	[UKagTagHandlerParamAttribute("opacity")]
	[UKagTagHandlerParamAttribute("color")]
	[UKagTagHandlerParamAttribute("materialcolor")]
	[UKagTagHandlerParamAttribute("visible")]
	public static int layopt(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		if (layerObj == null)
			return 0;
		
		string left = tag.GetParamValue("left");
		string top = tag.GetParamValue("top");
		string width = tag.GetParamValue("width");
		string height = tag.GetParamValue("height");
		string imageLeft = tag.GetParamValue("imageleft");
		string imageTop = tag.GetParamValue("imagetop");
		string imageWidth = tag.GetParamValue("imagewidth");
		string imageHeight = tag.GetParamValue("imageheight");
		string slice9Left = tag.GetParamValue("slice9left");
		string slice9Top = tag.GetParamValue("slice9top");
		string slice9Right = tag.GetParamValue("slice9right");
		string slice9Bottom = tag.GetParamValue("slice9bottom");
		string fliplr = tag.GetParamValue("fliplr");
		string flipud = tag.GetParamValue("flipud");
		string hittype = tag.GetParamValue("hittype");
		string hitthreshold = tag.GetParamValue("hitthreshold");
		string index = tag.GetParamValue("index");
		string opacity = tag.GetParamValue("opacity");
		string visible = tag.GetParamValue("visible");

		// Starting from the top right, clockwise, 1(top right), 2(right), 3(bottom right), 4(bottom),
		// 5(bottom left), 6(left), 7(top left, default), 8(top), 9(center)
		string origin = tag.GetParamValue("origin");
		string vorigin = tag.GetParamValue("vorigin");

		if (tag.GetParamValue("page") == "back")
		{
			if (visible != null)
				layerObj.BackVisible = m_System.Str2Bool(visible);
			layerObj.BackLayer.SetLayerOptions(left, top, width, height,
				imageLeft, imageTop, imageWidth, imageHeight,
				slice9Left, slice9Top, slice9Right, slice9Bottom,
			    fliplr, flipud, origin, vorigin,
				hittype, hitthreshold, index, opacity, null);
		}
		else
		{
			layerObj.SetLayerOptions(left, top, width, height,
				imageLeft, imageTop, imageWidth, imageHeight,
				slice9Left, slice9Top, slice9Right, slice9Bottom,
			    fliplr, flipud, origin, vorigin,
				hittype, hitthreshold, index, opacity, visible);
		}
		
		string animseq = tag.GetParamValue("animseq");
		string animidx = tag.GetParamValue("animidx");
		if (animseq != null && animidx != null)
			layerObj.SetAnimOption(animseq, "true", animidx);
		
		string color = tag.GetParamValue("color");
		if (color != null)
			layerObj.Color = UTVPUtil.StrToColor(color);
		
		string materialcolor = tag.GetParamValue("materialcolor");
		if (materialcolor != null)
			layerObj.MaterialColor = UTVPUtil.StrToColor(materialcolor);
			
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	[UKagTagHandlerParamAttribute("page")]
	public static int freeimage(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		if (layerObj == null)
			return 0;
		
		if (tag.GetParamValue("page") == "back")
			layerObj.ImageBack = null;
		else
			layerObj.Image = null;
		
		return 0;
	}
#endregion
#region MessageLayer
/**************************************************MessageLayer********************************************/
	[UKagTagHandlerParamAttribute("text", IsOptional=false)]
	public static int ch(UKagScenarioTag tag)
	{
		string text = tag.GetParamValue("text");
		int waittime = 0;
		if (text != null && text.Length > 0)
		{
			m_System.current.HideClickGlyphLayer();

			if (m_System.current.LineBreakMode == UTVPMessageLayer.LINE_BREAK_MODE.Alphabetic && text[text.Length - 1] == ' ')
				m_System.current.SetAlphabeticWord(m_System.conductor.TagList, m_System.conductor.CurrTagIdx + 1);

			waittime = m_System.current.AddCharacter(text[0]);
			if (waittime == 1)
				return m_System.current.ChWaits;
			else if (waittime == 2)
				return m_System.current.LineWaits;
			else if (waittime == 3)
				return m_System.current.PageWaits;
		}
		return waittime;
	}
	
	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	public static int current(UKagScenarioTag tag)
	{
		string layer = tag.GetParamValue("layer");
		if (layer == null || layer == "")
			return 0;
		
		if (layer.IndexOf("message") == 0)
			layer = layer.Substring(7);
		m_System.currMsgLayerIdx = Int32.Parse(layer);
		return 0;
	}
	
	public static int l(UKagScenarioTag tag)
	{
		m_System.current.ShowClickGlyphLayerLineBreak();
		return waitclick(tag);
	}
	public static int r(UKagScenarioTag tag)
	{
		return m_System.current.LineFeed();
	}
	public static int p(UKagScenarioTag tag)
	{
		m_System.current.ShowClickGlyphLayerPageBreak();
		return waitclick(tag);
	}
	public static int er(UKagScenarioTag tag)
	{
		return m_System.current.ClearContent();
	}
	public static int scrollup(UKagScenarioTag tag)
	{
		return m_System.current.ScrollUp();
	}
	
	[UKagTagHandlerParamAttribute("x")]
	[UKagTagHandlerParamAttribute("y")]
	public static int locate(UKagScenarioTag tag)
	{
		string strX = tag.GetParamValue("x");
		string strY = tag.GetParamValue("y");
		
		int x = m_System.current.LocateX;
		m_System.Str2Number(strX, ref x);
		int y = m_System.current.LocateY;
		m_System.Str2Number(strY, ref y);
		return m_System.current.SetLocate(x, y);
	}
	
	public static int nowait(UKagScenarioTag tag)
	{
		m_System.conductor.NoWaitForCh = true;
		return 0;
	}
	public static int endnowait(UKagScenarioTag tag)
	{
		m_System.conductor.NoWaitForCh = false;
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("frame")]
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("imageleft")]
	[UKagTagHandlerParamAttribute("imagetop")]
	[UKagTagHandlerParamAttribute("imagewidth")]
	[UKagTagHandlerParamAttribute("imageheight")]
	[UKagTagHandlerParamAttribute("slice9left")]
	[UKagTagHandlerParamAttribute("slice9top")]
	[UKagTagHandlerParamAttribute("slice9right")]
	[UKagTagHandlerParamAttribute("slice9bottom")]
	[UKagTagHandlerParamAttribute("fliplr")]
	[UKagTagHandlerParamAttribute("flipud")]
	[UKagTagHandlerParamAttribute("origin")]
	[UKagTagHandlerParamAttribute("vorigin")]
	[UKagTagHandlerParamAttribute("animseq")]
	[UKagTagHandlerParamAttribute("animidx")]
	[UKagTagHandlerParamAttribute("index")]
	[UKagTagHandlerParamAttribute("opacity")]
	[UKagTagHandlerParamAttribute("nmode")]
	[UKagTagHandlerParamAttribute("ermode")]
	[UKagTagHandlerParamAttribute("pitch")]
	[UKagTagHandlerParamAttribute("linespacing")]
	[UKagTagHandlerParamAttribute("scalex")]
	[UKagTagHandlerParamAttribute("scaley")]
	[UKagTagHandlerParamAttribute("font")]
	[UKagTagHandlerParamAttribute("color")]
	[UKagTagHandlerParamAttribute("shadowcolor")]
	[UKagTagHandlerParamAttribute("shadow")]
	[UKagTagHandlerParamAttribute("edge")]
	[UKagTagHandlerParamAttribute("marginl")]
	[UKagTagHandlerParamAttribute("margint")]
	[UKagTagHandlerParamAttribute("marginr")]
	[UKagTagHandlerParamAttribute("marginb")]
	[UKagTagHandlerParamAttribute("chwaits")]
	[UKagTagHandlerParamAttribute("linewaits")]
	[UKagTagHandlerParamAttribute("pagewaits")]
	[UKagTagHandlerParamAttribute("visible")]
	public static int position(UKagScenarioTag tag)
	{
		string frame = tag.GetParamValue("frame");
		string left = tag.GetParamValue("left");
		string top = tag.GetParamValue("top");
		string width = tag.GetParamValue("width");
		string height = tag.GetParamValue("height");
		string imageLeft = tag.GetParamValue("imageleft");
		string imageTop = tag.GetParamValue("imagetop");
		string imageWidth = tag.GetParamValue("imagewidth");
		string imageHeight = tag.GetParamValue("imageheight");
		string slice9Left = tag.GetParamValue("slice9left");
		string slice9Top = tag.GetParamValue("slice9top");
		string slice9Right = tag.GetParamValue("slice9right");
		string slice9Bottom = tag.GetParamValue("slice9bottom");
		string fliplr = tag.GetParamValue("fliplr");
		string flipud = tag.GetParamValue("flipud");
		string animseq = tag.GetParamValue("animseq");
		string animidx = tag.GetParamValue("animidx");
		string index = tag.GetParamValue("index");
		string opacity = tag.GetParamValue("opacity");

		// Starting from the top right, clockwise, 1(top right), 2(right), 3(bottom right), 4(bottom),
		// 5(bottom left), 6(left), 7(top left, default), 8(top), 9(center)
		string origin = tag.GetParamValue("origin");
		string vorigin = tag.GetParamValue("vorigin");

		string lineBreakMode = tag.GetParamValue("nmode");
		string pageBreakMode = tag.GetParamValue("ermode");
		string pitch = tag.GetParamValue("pitch");
		string lineSpacing = tag.GetParamValue("linespacing");
		string scaleX = tag.GetParamValue("scalex");
		string scaleY = tag.GetParamValue("scaley");
		string fontFace = tag.GetParamValue("font");
		string messageColor = tag.GetParamValue("color");
		string shadowColor = tag.GetParamValue("shadowcolor");
		string shadow = tag.GetParamValue("shadow");
		string edge = tag.GetParamValue("edge");
		string marginLeft = tag.GetParamValue("marginl");
		string marginTop = tag.GetParamValue("margint");
		string marginRight = tag.GetParamValue("marginr");
		string marginBottom = tag.GetParamValue("marginb");
		string chWaits = tag.GetParamValue("chwaits");
		string lineWaits = tag.GetParamValue("linewaits");
		string pageWaits = tag.GetParamValue("pagewaits");
		string visible = tag.GetParamValue("visible");
		
		UKagMessageLayer currMsg = m_System.current;
		string layer = tag.GetParamValue("layer");
		if (layer != null)
		{
			if (layer.IndexOf("message") == 0)
				layer = layer.Substring(7);
			int currMsgIdx = Int32.Parse(layer);
			if (currMsgIdx >= 0 && currMsgIdx < m_System.MsgLayers.Length)
				currMsg = m_System.MsgLayers[currMsgIdx];
		}
		
		if (frame != null)
		{
			UTVPImage frameImg = m_Storages.GetImage(frame);
			if (frameImg != null)
				currMsg.Frame.LoadImage(frameImg);
			else
				currMsg.Frame.Image = null;
		}
		if (currMsg.Frame is UKagLayer)
		{
			((UKagLayer)currMsg.Frame).SetLayerOptions(left, top, width, height,
				imageLeft, imageTop, imageWidth, imageHeight,
				slice9Left, slice9Top, slice9Right, slice9Bottom,
			    fliplr, flipud, origin, vorigin,
				null, null, index, opacity, null);
			if (animseq != null && animidx != null)
				((UKagLayer)currMsg.Frame).SetAnimOption(animseq, true, System.Int32.Parse(animidx), width == null && height == null);
		}
		
		currMsg.SetOptions(lineBreakMode, pageBreakMode, pitch, lineSpacing, scaleX, scaleY, fontFace,
			messageColor, shadowColor, shadow, edge, marginLeft, marginTop, marginRight, marginBottom,
			chWaits, lineWaits, pageWaits, visible);
		
		return 0;
	}
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("frame")]
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("imageleft")]
	[UKagTagHandlerParamAttribute("imagetop")]
	[UKagTagHandlerParamAttribute("imagewidth")]
	[UKagTagHandlerParamAttribute("imageheight")]
	[UKagTagHandlerParamAttribute("slice9left")]
	[UKagTagHandlerParamAttribute("slice9top")]
	[UKagTagHandlerParamAttribute("slice9right")]
	[UKagTagHandlerParamAttribute("slice9bottom")]
	[UKagTagHandlerParamAttribute("fliplr")]
	[UKagTagHandlerParamAttribute("flipud")]
	[UKagTagHandlerParamAttribute("origin")]
	[UKagTagHandlerParamAttribute("animseq")]
	[UKagTagHandlerParamAttribute("animidx")]
	[UKagTagHandlerParamAttribute("index")]
	[UKagTagHandlerParamAttribute("opacity")]
	[UKagTagHandlerParamAttribute("nmode")]
	[UKagTagHandlerParamAttribute("ermode")]
	[UKagTagHandlerParamAttribute("pitch")]
	[UKagTagHandlerParamAttribute("linespacing")]
	[UKagTagHandlerParamAttribute("scalex")]
	[UKagTagHandlerParamAttribute("scaley")]
	[UKagTagHandlerParamAttribute("font")]
	[UKagTagHandlerParamAttribute("color")]
	[UKagTagHandlerParamAttribute("shadowcolor")]
	[UKagTagHandlerParamAttribute("shadow")]
	[UKagTagHandlerParamAttribute("edge")]
	[UKagTagHandlerParamAttribute("marginl")]
	[UKagTagHandlerParamAttribute("margint")]
	[UKagTagHandlerParamAttribute("marginr")]
	[UKagTagHandlerParamAttribute("marginb")]
	[UKagTagHandlerParamAttribute("chwaits")]
	[UKagTagHandlerParamAttribute("linewaits")]
	[UKagTagHandlerParamAttribute("pagewaits")]
	[UKagTagHandlerParamAttribute("visible")]
	public static int msgopt(UKagScenarioTag tag)
	{
		return position(tag);
	}
	
	[UKagTagHandlerParamAttribute("storage", IsOptional=false)]
	[UKagTagHandlerParamAttribute("reline")]
	[UKagTagHandlerParamAttribute("repage")]
	[UKagTagHandlerParamAttribute("fix")]
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	public static int glyph(UKagScenarioTag tag)
	{
		string storage = tag.GetParamValue("storage");
		if (storage == null)
			storage = tag.GetParamValue("file");
		UTVPImage img = m_Storages.GetImage(storage);
		
		string relineAnim = tag.GetParamValue("reline");
		string repageAnim = tag.GetParamValue("repage");
		string fix = tag.GetParamValue("fix");
		string left = tag.GetParamValue("left");
		string top = tag.GetParamValue("top");
		string width = tag.GetParamValue("width");
		string height = tag.GetParamValue("height");

		m_System.current.SetClickGlyphLayerInfo(img, relineAnim, repageAnim, fix, left, top, width, height);
		
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("font")]
	[UKagTagHandlerParamAttribute("color")]
	[UKagTagHandlerParamAttribute("frameimg")]
	[UKagTagHandlerParamAttribute("frameseq")]
	[UKagTagHandlerParamAttribute("frameidx")]
	[UKagTagHandlerParamAttribute("marginl")]
	[UKagTagHandlerParamAttribute("margint")]
	[UKagTagHandlerParamAttribute("marginr")]
	[UKagTagHandlerParamAttribute("marginb")]
	[UKagTagHandlerParamAttribute("nameleft")]
	[UKagTagHandlerParamAttribute("nametop")]
	[UKagTagHandlerParamAttribute("nameimg")]
	[UKagTagHandlerParamAttribute("nameseq")]
	[UKagTagHandlerParamAttribute("namelist")]
	[UKagTagHandlerParamAttribute("clear")]
	public static int nameopt(UKagScenarioTag tag)
	{
		string left = tag.GetParamValue("left");
		string top = tag.GetParamValue("top");
		string width = tag.GetParamValue("width");
		string height = tag.GetParamValue("height");
		string font = tag.GetParamValue("font");
		string color = tag.GetParamValue("color");
		string marginl = tag.GetParamValue("marginl");
		string margint = tag.GetParamValue("margint");
		string marginr = tag.GetParamValue("marginr");
		string marginb = tag.GetParamValue("marginb");
		string frameimg = tag.GetParamValue("frameimg");
		string frameseq = tag.GetParamValue("frameseq");
		string frameidx = tag.GetParamValue("frameidx");
		string nameleft = tag.GetParamValue("nameleft");
		string nametop = tag.GetParamValue("nametop");
		string nameimg = tag.GetParamValue("nameimg");
		string nameseq = tag.GetParamValue("nameseq");
		string namelist = tag.GetParamValue("namelist");
		string clear = tag.GetParamValue("clear");
		
		m_System.current.SetNameLayerOpt(left, top, width, height, frameimg, frameseq, frameidx,
			marginl, margint, marginr, marginb, font, color);
		m_System.current.SetNameImage(nameleft, nametop, nameimg, nameseq, namelist, clear);
		return 0;
	}
	[UKagTagHandlerParamAttribute("text")]
	[UKagTagHandlerParamAttribute("clear")]
	public static int name(UKagScenarioTag tag)
	{	
		string text = tag.GetParamValue("text");
		string clear = tag.GetParamValue("clear");
		if (text != null && text.IndexOf('/') >= 0)
			text = text.Substring(text.IndexOf('/') + 1);

		if (text != null)
			m_System.current.SetName(text);
		if (m_System.Str2Bool(clear))
			m_System.current.SetName(null);
		
		return 0;
	}
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("faceimg")]
	public static int faceopt(UKagScenarioTag tag)
	{
		string left = tag.GetParamValue("left");
		string top = tag.GetParamValue("top");
		string width = tag.GetParamValue("width");
		string height = tag.GetParamValue("height");
		string faceimg = tag.GetParamValue("faceimg");
		
		m_System.current.SetFaceLayerOpt(left, top, width, height, faceimg);
		
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("faceseq")]
	[UKagTagHandlerParamAttribute("faceidx")]
	[UKagTagHandlerParamAttribute("clear")]
	public static int face(UKagScenarioTag tag)
	{
		string faceseq = tag.GetParamValue("faceseq");
		string faceidx = tag.GetParamValue("faceidx");
		string clear = tag.GetParamValue("clear");
		
		if (m_System.Str2Bool(clear))
			m_System.current.SetFace(null, 0);
		else
			m_System.current.SetFace(faceseq, faceidx);
		
		return 0;
	}
#endregion
#region Action
/****************************************************Action*************************************************/	
	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	[UKagTagHandlerParamAttribute("type")]
	[UKagTagHandlerParamAttribute("stop")]
	[UKagTagHandlerParamAttribute("pause")]
	[UKagTagHandlerParamAttribute("resume")]
	[UKagTagHandlerParamAttribute("time")]
	[UKagTagHandlerParamAttribute("isglobal")]
	[UKagTagHandlerParamAttribute("ignoretimescale")]
	[UKagTagHandlerParamAttribute("easetype")]
	[UKagTagHandlerParamAttribute("path")]
	[UKagTagHandlerParamAttribute("color")]
	[UKagTagHandlerParamAttribute("alpha")]
	public static int action(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		string type = tag.GetParamValue("type");

		string stop = tag.GetParamValue("stop");
		string pause = tag.GetParamValue("pause");
		string resume = tag.GetParamValue("resume");
		
		if (stop == "true")
			m_System.actionHandler.StopAction(layerObj, type);
		else if (pause == "true")
			m_System.actionHandler.PauseAction(layerObj);
		else if (resume == "true")
			m_System.actionHandler.ResumeAction(layerObj);
		else if (layerObj != null)
			m_System.actionHandler.StartAction(layerObj, tag.GetParamDic());

		return 0;
	}
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("type")]
	[UKagTagHandlerParamAttribute("canskip")]
	public static int waitaction(UKagScenarioTag tag)
	{
		string canskip = tag.GetParamValue("canskip");
		UKagLayer layerObj = GetLayerByTagParam(tag);
		string type = tag.GetParamValue("type");

		m_System.actionHandler.WaitAction(m_System.conductor, layerObj, type, canskip == "true");
		return 0;
	}
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("canskip")]
	public static int wact(UKagScenarioTag tag)
	{
		return waitaction(tag);
	}
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("type")]
	public static int stopaction(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		string type = tag.GetParamValue("type");

		m_System.actionHandler.StopAction(layerObj, type);
		return 0;
	}
#endregion
#region Trans
/****************************************************Trans*************************************************/	
	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	[UKagTagHandlerParamAttribute("time", IsOptional=false)]
	[UKagTagHandlerParamAttribute("method")]
	[UKagTagHandlerParamAttribute("rule")]
	[UKagTagHandlerParamAttribute("vague")]
	public static int trans(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		string timeStr = tag.GetParamValue("time");
		if (layerObj == null || timeStr == null)
			return 0;
		int nTime = 2000;
		m_System.Str2Number(timeStr, ref nTime);
		
		string transType = tag.GetParamValue("method");
		if (string.IsNullOrEmpty(transType) || transType == "crossfade")
		{
			layerObj.BeginTransition(UKagLayer.TRANS_TYPE.CROSSFADE, nTime / 1000.0f, null, 0);
		}
		else if (transType == "universal")
		{
			string rule = tag.GetParamValue("rule");
			UTVPImage ruleImg = m_Storages.GetImage(rule);
			string vagueStr = tag.GetParamValue("vague");
			float vague = 0;
			m_System.Str2Number(vagueStr, ref vague);

			layerObj.BeginTransition(UKagLayer.TRANS_TYPE.UNIVERSAL, nTime / 1000.0f, ruleImg, vague);
		}
		
		return 0;
	}
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("canskip")]
	public static int waittrans(UKagScenarioTag tag)
	{
		string canskip = tag.GetParamValue("canskip");
		UKagLayer layerObj = GetLayerByTagParam(tag);
		
		m_System.conductor.WaitTrigger("_trans" + (layerObj != null ? "_" + layerObj.name : ""), canskip == "true");
		return 0;
	}
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("canskip")]
	public static int wt(UKagScenarioTag tag)
	{
		return waittrans(tag);
	}
	[UKagTagHandlerParamAttribute("layer")]
	public static int stoptrans(UKagScenarioTag tag)
	{
		string layer = tag.GetParamValue("layer");
		m_System.StopTrans(layer);
		
		return 0;
	}
#endregion
#region Animation
/****************************************************Animation*************************************************/	
	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	[UKagTagHandlerParamAttribute("name", IsOptional=false)]
	[UKagTagHandlerParamAttribute("pause")]
	[UKagTagHandlerParamAttribute("curridx")]
	public static int animopt(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		if (layerObj == null)
			return 0;
		
		string animName = tag.GetParamValue("target");
		if (animName == null)
			animName = tag.GetParamValue("name");
		string animPause = tag.GetParamValue("pause");
		string currFrameIdx = tag.GetParamValue("curridx");
		
		layerObj.SetAnimOption(animName, animPause, currFrameIdx);
		return 0;
	}
	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	[UKagTagHandlerParamAttribute("name", IsOptional=false)]
	[UKagTagHandlerParamAttribute("pause")]
	[UKagTagHandlerParamAttribute("curridx")]
	public static int startanim(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		if (layerObj == null)
			return 0;

		string animName = tag.GetParamValue("target");
		if (animName == null)
			animName = tag.GetParamValue("name");
		string currFrameIdxStr = tag.GetParamValue("curridx");
		string animPauseStr = tag.GetParamValue("pause");
		int currFrameIdx = layerObj.CurrFrameIdx;
		bool animPause = false;
		m_System.Str2Number(currFrameIdxStr, ref currFrameIdx);
		if (animPauseStr != null)
			animPause = animPauseStr == "true";
		
		layerObj.StartAnimation(animName, currFrameIdx, animPause);
		
		return 0;
	}
	[UKagTagHandlerParamAttribute("layer", IsOptional=false)]
	[UKagTagHandlerParamAttribute("curridx")]
	public static int stopanim(UKagScenarioTag tag)
	{
		UKagLayer layerObj = GetLayerByTagParam(tag);
		if (layerObj == null)
			return 0;
		
		string currFrameIdxStr = tag.GetParamValue("curridx");
		
		layerObj.AnimPause = true;
		if (currFrameIdxStr != null)
			layerObj.CurrFrameIdx = Int32.Parse(currFrameIdxStr);
		
		return 0;
	}
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("canskip")]
	public static int waitanim(UKagScenarioTag tag)
	{
		string canskip = tag.GetParamValue("canskip");
		UKagLayer layerObj = GetLayerByTagParam(tag);
		
		m_System.conductor.WaitTrigger("_anim" + (layerObj != null ? "_" + layerObj.name : ""), canskip == "true");
		return 0;
	}
	[UKagTagHandlerParamAttribute("layer")]
	[UKagTagHandlerParamAttribute("canskip")]
	public static int wa(UKagScenarioTag tag)
	{
		return waitanim(tag);
	}
#endregion
#region Audio/Vedio
/***************************************************Audio and Vedio******************************************************/
	[UKagTagHandlerParamAttribute("storage")]
	[UKagTagHandlerParamAttribute("volume")]
	[UKagTagHandlerParamAttribute("pan")]
	[UKagTagHandlerParamAttribute("loop")]
	[UKagTagHandlerParamAttribute("stop")]
	[UKagTagHandlerParamAttribute("pause")]
	[UKagTagHandlerParamAttribute("resume")]
	public static int bgm(UKagScenarioTag tag)
	{
		if (tag.GetParamValue("stop") == "true")
			m_System.BgmBuf.stop();
		else if (tag.GetParamValue("pause") == "true")
			m_System.BgmBuf.paused = true;
		else if (tag.GetParamValue("resume") == "true")
			m_System.BgmBuf.paused = false;
		else
			playbgm(tag);
		return 0;
	}
	[UKagTagHandlerParamAttribute("storage", IsOptional=false)]
	[UKagTagHandlerParamAttribute("volume")]
	[UKagTagHandlerParamAttribute("pan")]
	[UKagTagHandlerParamAttribute("loop")]
	public static int playbgm(UKagScenarioTag tag)
	{
		string volume = tag.GetParamValue("volume");
		if (volume != null && volume != "")
		{
			int volumeInt = m_System.BgmBuf.volume;
			m_System.Str2Number(volume, ref volumeInt);
			m_System.BgmBuf.volume = volumeInt;
		}
		else
			m_System.BgmBuf.volume = 100;
		string pan = tag.GetParamValue("pan");
		if (pan != null && pan != "")
		{
			int panInt = m_System.BgmBuf.pan;
			m_System.Str2Number(pan, ref panInt);
			m_System.BgmBuf.pan = panInt;
		}
			
		string storage = tag.GetParamValue("storage");
		if (storage == null)
			storage = tag.GetParamValue("file");
		if (storage != null && storage != "")
		{
			UTVPAudio audioFile = m_Storages.GetAudio(storage);
			if (audioFile != null)
				m_System.BgmBuf.play(audioFile);

			m_System.BgmBuf.looping = tag.GetParamValue("loop") != "false";
		}
		return 0;
	}
	
	public static int stopbgm(UKagScenarioTag tag)
	{
		m_System.BgmBuf.stop();
		return 0;
	}
	
	public static int pausebgm(UKagScenarioTag tag)
	{
		m_System.BgmBuf.paused = true;
		return 0;
	}
	
	public static int resumebgm(UKagScenarioTag tag)
	{
		m_System.BgmBuf.paused = false;
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("volume")]
	[UKagTagHandlerParamAttribute("time")]
	public static int fadebgm(UKagScenarioTag tag)
	{
		string volume = tag.GetParamValue("volume");
		string time = tag.GetParamValue("time");
		m_System.BgmBuf.Fade(volume, time);
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("storage", IsOptional=false)]
	[UKagTagHandlerParamAttribute("time", IsOptional=false)]
	[UKagTagHandlerParamAttribute("loop")]
	[UKagTagHandlerParamAttribute("volume")]
	public static int fadeinbgm(UKagScenarioTag tag)
	{
		string storage = tag.GetParamValue("storage");
		string loop = tag.GetParamValue("loop");
		string volume = tag.GetParamValue("volume");
		string time = tag.GetParamValue("time");
		if (string.IsNullOrEmpty(volume))
			volume = "100";
		m_System.BgmBuf.looping = loop != "false";
		
		if (m_System.BgmBuf.volume == 0)
		{
			UTVPAudio audioFile = m_Storages.GetAudio(storage);
			if (audioFile == null)
				return 0;
			m_System.BgmBuf.play(audioFile);
			m_System.BgmBuf.Fade(volume, time);
		}
		else
		{
			m_System.BgmBuf.FadeToNextAudio(storage, volume, time);
		}
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("time", IsOptional=false)]
	[UKagTagHandlerParamAttribute("volume")]
	public static int fadeoutbgm(UKagScenarioTag tag)
	{
		string volume = tag.GetParamValue("volume");
		string time = tag.GetParamValue("time");
		if (string.IsNullOrEmpty(volume))
			volume = "0";
		m_System.BgmBuf.Fade(volume, time);
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("storage")]
	[UKagTagHandlerParamAttribute("buf")]
	[UKagTagHandlerParamAttribute("volume")]
	[UKagTagHandlerParamAttribute("pan")]
	[UKagTagHandlerParamAttribute("loop")]
	[UKagTagHandlerParamAttribute("stop")]
	[UKagTagHandlerParamAttribute("pause")]
	[UKagTagHandlerParamAttribute("resume")]
	public static int se(UKagScenarioTag tag)
	{
		int seIdx = 0;
		string buf = tag.GetParamValue("buf");
		if (buf != null && buf != "")
			m_System.Str2Number(buf, ref seIdx);
		
		if (seIdx >= m_System.SeBufs.Length)
			return 0;

		if (tag.GetParamValue("stop") == "true")
			m_System.SeBufs[seIdx].stop();
		else if (tag.GetParamValue("pause") == "true")
			m_System.SeBufs[seIdx].paused = true;
		else if (tag.GetParamValue("resume") == "true")
			m_System.SeBufs[seIdx].paused = false;
		else
			playseImpl(seIdx, tag);

		return 0;
	}
	
	[UKagTagHandlerParamAttribute("storage", IsOptional=false)]
	[UKagTagHandlerParamAttribute("buf")]
	[UKagTagHandlerParamAttribute("volume")]
	[UKagTagHandlerParamAttribute("pan")]
	[UKagTagHandlerParamAttribute("loop")]
	public static int playse(UKagScenarioTag tag)
	{
		int seIdx = 0;
		string buf = tag.GetParamValue("buf");
		if (buf != null && buf != "")
			m_System.Str2Number(buf, ref seIdx);
		
		if (seIdx >= m_System.SeBufs.Length)
			return 0;
		
		playseImpl(seIdx, tag);
		return 0;
	}
	
	private static void playseImpl(int seIdx, UKagScenarioTag tag)
	{
		string volume = tag.GetParamValue("volume");
		if (volume != null && volume != "")
		{
			int volumeInt = m_System.SeBufs[seIdx].volume;
			m_System.Str2Number(volume, ref volumeInt);
			m_System.SeBufs[seIdx].volume = volumeInt;
		}
		else
			m_System.SeBufs[seIdx].volume = 100;
		string pan = tag.GetParamValue("pan");
		if (pan != null && pan != "")
		{
			int panInt = m_System.SeBufs[seIdx].pan;
			m_System.Str2Number(pan, ref panInt);
			m_System.SeBufs[seIdx].pan = panInt;
		}

		string storage = tag.GetParamValue("storage");
		if (storage == null)
			storage = tag.GetParamValue("file");
		if (storage != null && storage != "")
		{
			UTVPAudio audioFile = m_Storages.GetAudio(storage);
			if (audioFile != null)
				m_System.SeBufs[seIdx].play(audioFile);

			m_System.SeBufs[seIdx].looping = tag.GetParamValue("loop") == "true";
		}
	}
	
	[UKagTagHandlerParamAttribute("buf")]
	public static int stopse(UKagScenarioTag tag)
	{
		int seIdx = 0;
		string buf = tag.GetParamValue("buf");
		if (buf != null && buf != "")
			m_System.Str2Number(buf, ref seIdx);
		
		if (seIdx >= m_System.SeBufs.Length)
			return 0;

		m_System.SeBufs[seIdx].stop();
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("buf")]
	public static int pausese(UKagScenarioTag tag)
	{
		int seIdx = 0;
		string buf = tag.GetParamValue("buf");
		if (buf != null && buf != "")
			m_System.Str2Number(buf, ref seIdx);
		
		if (seIdx >= m_System.SeBufs.Length)
			return 0;

		m_System.SeBufs[seIdx].paused = true;
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("buf")]
	public static int resumese(UKagScenarioTag tag)
	{
		int seIdx = 0;
		string buf = tag.GetParamValue("buf");
		if (buf != null && buf != "")
			m_System.Str2Number(buf, ref seIdx);
		
		if (seIdx >= m_System.SeBufs.Length)
			return 0;

		m_System.SeBufs[seIdx].paused = false;
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("storage", IsOptional=false)]
	[UKagTagHandlerParamAttribute("volume")]
	public static int voice(UKagScenarioTag tag)
	{
		string volume = tag.GetParamValue("volume");
		if (volume != null && volume != "")
		{
			int volumeInt = m_System.VoiceBuf.volume;
			m_System.Str2Number(volume, ref volumeInt);
			m_System.VoiceBuf.volume = volumeInt;
		}
		else
			m_System.VoiceBuf.volume = 100;
		
		string storage = tag.GetParamValue("storage");
		AudioClip voiceClip = m_Storages.GetVoice(storage);
		m_System.VoiceBuf.play(voiceClip);
		
		return 0;
	}
#endregion
#region Control/Logic
/***************************************************Control and Logic****************************************************/
	[UKagTagHandlerParamAttribute("name", IsOptional=false)]
	public static int delcontrol(UKagScenarioTag tag)
	{
		string name = tag.GetParamValue("name");
		if (name == null)
			return 0;
		m_System.current.DeleteControl(name);
		return 0;
	}
	
	[UKagTagHandlerParamAttribute("all")]
	public static int clearcontrols(UKagScenarioTag tag)
	{
		string all = tag.GetParamValue("all");
		m_System.current.ClearControls(all == "true");
		return 0;
	}

	[UKagTagHandlerParamAttribute("name", IsOptional=false)]
	[UKagTagHandlerParamAttribute("graphic", IsOptional=false)]
	[UKagTagHandlerParamAttribute("type")]
	[UKagTagHandlerParamAttribute("seq")]
	[UKagTagHandlerParamAttribute("isanim")]
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("storage")]
	[UKagTagHandlerParamAttribute("target")]
	[UKagTagHandlerParamAttribute("exp")]
	[UKagTagHandlerParamAttribute("clickse")]
	[UKagTagHandlerParamAttribute("enterse")]
	[UKagTagHandlerParamAttribute("leavese")]
	[UKagTagHandlerParamAttribute("visible")]
	public static int button(UKagScenarioTag tag)
	{
		return buttonImpl(tag, false);
	}
	[UKagTagHandlerParamAttribute("name", IsOptional=false)]
	[UKagTagHandlerParamAttribute("graphic", IsOptional=false)]
	[UKagTagHandlerParamAttribute("type")]
	[UKagTagHandlerParamAttribute("seq")]
	[UKagTagHandlerParamAttribute("isanim")]
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("storage")]
	[UKagTagHandlerParamAttribute("target")]
	[UKagTagHandlerParamAttribute("exp")]
	[UKagTagHandlerParamAttribute("clickse")]
	[UKagTagHandlerParamAttribute("enterse")]
	[UKagTagHandlerParamAttribute("leavese")]
	[UKagTagHandlerParamAttribute("visible")]
	public static int sysbutton(UKagScenarioTag tag)
	{
		return buttonImpl(tag, true);
	}
	private static int buttonImpl(UKagScenarioTag tag, bool isSys)
	{
		string name = tag.GetParamValue("name");
		if (name == null)
			return 0;
		
		UKagButton targetBtn = null;
		if (m_System.current.Controls.ContainsKey(name) && m_System.current.Controls[name] is UKagButton)
			targetBtn = (UKagButton)m_System.current.Controls[name];
		else
			targetBtn = m_System.current.CreateControl<UKagButton>(name, 10);
		
		string visible = tag.GetParamValue("visible");
		if (visible != null)
		{
			if (visible == "true")
				targetBtn.layer.Visible = true;
			else if (visible == "false")
				targetBtn.layer.Visible = false;
		}

		string graphic = tag.GetParamValue("graphic");
		if (graphic == null)
			return 0;
		
		UTVPImage img = m_Storages.GetImage(graphic);
		if (img == null || targetBtn == null)
			return 0;
		
		string type = tag.GetParamValue("type");
		string seq = tag.GetParamValue("seq");
		string isanim = tag.GetParamValue("isanim");
		string left = tag.GetParamValue("left");
		string top = tag.GetParamValue("top");
		string width = tag.GetParamValue("width");
		string height = tag.GetParamValue("height");
		if (string.IsNullOrEmpty(seq))
			seq = name;

		targetBtn.SetButtonOpt(isSys, img, type, seq, isanim, left, top, width, height);
		
		string storage = tag.GetParamValue("storage");
		string target = tag.GetParamValue("target");
		string exp = tag.GetParamValue("exp");
		//string onenter = tag.GetParamValue("onenter");
		//string onleave = tag.GetParamValue("onleave");
		string clickseStr = tag.GetParamValue("clickse");
		string enterseStr = tag.GetParamValue("enterse");
		string leaveseStr = tag.GetParamValue("leavese");

		targetBtn.targetScenario = storage;
		targetBtn.targetLabel = target;
		UTVPAudio clickse = m_Storages.GetAudio(clickseStr);
		UTVPAudio enterse = m_Storages.GetAudio(enterseStr);
		UTVPAudio leavese = m_Storages.GetAudio(leaveseStr);
		targetBtn.clickSE = clickse != null ? clickse.Clip : null;
		targetBtn.enterSE = enterse != null ? enterse.Clip : null;
		targetBtn.leaveSE = leavese != null ? leavese.Clip : null;
		targetBtn.eventParam = exp;

		return 0;
	}
	
	[UKagTagHandlerParamAttribute("name", IsOptional=false)]
	[UKagTagHandlerParamAttribute("graphic", IsOptional=false)]
	[UKagTagHandlerParamAttribute("seq")]
	[UKagTagHandlerParamAttribute("left")]
	[UKagTagHandlerParamAttribute("top")]
	[UKagTagHandlerParamAttribute("width")]
	[UKagTagHandlerParamAttribute("height")]
	[UKagTagHandlerParamAttribute("tabwidth")]
	[UKagTagHandlerParamAttribute("tabheight")]
	[UKagTagHandlerParamAttribute("vertical")]
	[UKagTagHandlerParamAttribute("min")]
	[UKagTagHandlerParamAttribute("max")]
	[UKagTagHandlerParamAttribute("value")]
	[UKagTagHandlerParamAttribute("step")]
	[UKagTagHandlerParamAttribute("property")]
	[UKagTagHandlerParamAttribute("clickse")]
	[UKagTagHandlerParamAttribute("enterse")]
	[UKagTagHandlerParamAttribute("leavese")]
	[UKagTagHandlerParamAttribute("visible")]
	public static int slider(UKagScenarioTag tag)
	{
		string name = tag.GetParamValue("name");
		if (name == null)
			return 0;
		
		UKagSlider targetSlider = null;
		if (m_System.current.Controls.ContainsKey(name) && m_System.current.Controls[name] is UKagSlider)
			targetSlider = (UKagSlider)m_System.current.Controls[name];
		else
			targetSlider = m_System.current.CreateControl<UKagSlider>(name, 10);
		
		string visible = tag.GetParamValue("visible");
		if (visible != null)
		{
			if (visible == "true")
				targetSlider.layer.Visible = true;
			else if (visible == "false")
				targetSlider.layer.Visible = false;
		}

		string graphic = tag.GetParamValue("graphic");
		if (graphic == null)
			return 0;
		
		UTVPImage img = m_Storages.GetImage(graphic);
		if (img == null || targetSlider == null)
			return 0;
		
		string seq = tag.GetParamValue("seq");
		string left = tag.GetParamValue("left");
		string top = tag.GetParamValue("top");
		string width = tag.GetParamValue("width");
		string height = tag.GetParamValue("height");
		string tabWidth = tag.GetParamValue("tabwidth");
		string tabHeight = tag.GetParamValue("tabheight");
		string vertical = tag.GetParamValue("vertical");

		targetSlider.SetSliderOpt(img, seq, vertical, left, top, width, height, tabWidth, tabHeight);
		
		string minStr = tag.GetParamValue("min");
		string maxStr = tag.GetParamValue("max");
		string valueStr = tag.GetParamValue("value");
		float min = targetSlider.minValue;
		float max = targetSlider.maxValue;
		float value = targetSlider.currValue;
		m_System.Str2Number(minStr, ref min);
		m_System.Str2Number(maxStr, ref max);
		m_System.Str2Number(valueStr, ref value);
		targetSlider.setValue(min, max, value);
		string step = tag.GetParamValue("step");
		m_System.Str2Number(step, ref targetSlider.step);
		
		string clickseStr = tag.GetParamValue("clickse");
		string enterseStr = tag.GetParamValue("enterse");
		string leaveseStr = tag.GetParamValue("leavese");
		UTVPAudio clickse = m_Storages.GetAudio(clickseStr);
		UTVPAudio enterse = m_Storages.GetAudio(enterseStr);
		UTVPAudio leavese = m_Storages.GetAudio(leaveseStr);
		targetSlider.clickSE = clickse != null ? clickse.Clip : null;
		targetSlider.enterSE = enterse != null ? enterse.Clip : null;
		targetSlider.leaveSE = leavese != null ? leavese.Clip : null;
		
		string property = tag.GetParamValue("property");
		//string onenter = tag.GetParamValue("onenter");
		//string onleave = tag.GetParamValue("onleave");
		targetSlider.SetValueProperty(property);

		return 0;
	}
#endregion
}
